package com.ubao.network;

import java.io.DataOutputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;

import android.util.Log;

import com.ubao.network.req.SpecialRequest;
import com.ubao.util.MD5;
import com.ubao.util.log.Logger;


public class HttpHelper {
	
	public static SpecialRequest getMultPartRequest(String actionUrl, Map<String, String> params,    
			Map<String, File> files) {
		final SpecialRequest request = new SpecialRequest(actionUrl);
		request.files = files;
		request.encoding = "UTF-8";
		
		String BOUNDARY = java.util.UUID.randomUUID().toString();    
		String PREFIX = "--" , LINEND = "\r\n";    
		String MULTIPART_FROM_DATA = "multipart/form-data";    
		String CHARSET = "UTF-8";    
		request.boundary = BOUNDARY;
		Hashtable requestProperties = new Hashtable(3);
		requestProperties.put("connection","keep-alive");
		requestProperties.put("Charsert","UTF-8");
		requestProperties.put("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
		request.requestProperties = requestProperties;
		  
		// 首先组拼文本类型的参数    
		StringBuilder sb = new StringBuilder();    
		for (Map.Entry<String, String> entry : params.entrySet()) {    
			sb.append(PREFIX);    
			sb.append(BOUNDARY);    
			sb.append(LINEND);    
			sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);    
			sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);    
			sb.append("Content-Transfer-Encoding: 8bit" + LINEND);    
			sb.append(LINEND);    
			sb.append(entry.getValue());    
			sb.append(LINEND);    
		}    
		request.postParams = sb.toString();
		Logger.d("params-->" + request.postParams);
		return request;
	}
}
